
package stcu.control;

import stcu.*;
import stcu.forms.*;
import stcu.robjects.*;
import com.nvlled.route.*;

import java.util.*;
import javax.swing.*;
import java.io.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;

public class ControlledChatForm extends ChatForm
    implements Group.Listener {

    private JFrame self = this;
    private Group group;
    private DefaultListModel<Account> userListModel;
    private FileManager fileMan;
    private FileManagerForm fileManForm;

    public ControlledChatForm(ObjectRouter router, Group group) {
        this.group = group;
        userListModel = new DefaultListModel<Account>();

        messageLogText.setEditable(false);
        memberList.setModel(userListModel);
        group.addListener(this);

        handleButtons();
        group.listMembers();
		
		jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		messageLogText.setLineWrap(true);
		
		DefaultCaret caret = (DefaultCaret)messageLogText.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        try {
            fileMan = new FileManager(router, group);
            fileManForm = new FileManagerForm(fileMan);
            fileMan.listFiles();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(self, "Filemanager failed.");
        }
    }

    private void handleButtons() {
        fileButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fileManForm.setVisible(true);
            }
        });
        sendButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String message = messageText.getText();
				if (message.isEmpty()){
					return;
				}
                messageText.setText("");
                group.send(message);
            }
        });
        leaveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                group.leave();
                setVisible(false);
            }
        });
		
		messageText.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String message = messageText.getText();
				if (message.isEmpty()){
					return;
				}
                messageText.setText("");
                group.send(message);
			}
		});

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                fileMan.stop();
                group.leave();
                setVisible(false);
            }
        });
    }

    public void messageReceived(GroupMessage message) {
        Calendar cal = Calendar.getInstance();
		cal.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss aa");
		
		/**
			(d MM yyyy)
			
			
			import java.text.SimpleDateFormat;
			DATE_FORMAT = new SimpleDateFormat("dd/MM/yy");
			String date = DATE_FORMAT.format(today);
			System.out.println(date);
			
			(MMM d, ''yy)
		
		**/
		
		
		String line = message.getSender() + ": " +
            message.getMessage();
		messageLogText.append(line+"\n\n");
		messageLogText.append("\t"+"\t"+"\t"+"               "+"Received "+sdf.format(cal.getTime())+"\n");
    }

    public void memberJoined(MemberAdd member) {
        String name = member.getName();
        if (member.isNewMember()) {
            String line = name + " has joined the group";
            messageLogText.append(line+"\n\n");
        }
        if (! userListModel.contains(name)) {
            userListModel.addElement(member.getAccount());
        }
    }

    public void memberLeft(MemberDel member) {
        System.out.println("%%");
        String line = member.getName() + " has left the group";
        messageLogText.append(line+"\n\n");
        userListModel.removeElement(member.getAccount());
    }

}
